package webshop.web;

import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;

import webshop.data.Person;
import webshop.web.PersonUtil;

/**
 * Handles a Register form request.
 * @author Simon & ohm.
 */
public class PersonRegisterServlet extends AbstractSuperServlet {

    @Resource
    UserTransaction tx;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        EntityManager em = emf.createEntityManager();
        PrintWriter out = response.getWriter();

        String personName = null;
        String personAddress = null;
        long personPhone = 0;
        String empty = request.getParameter(null);

        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Expires", "0");


        try {
            // Get paramters.
            personName = request.getParameter("personname");
            personAddress = request.getParameter("personaddress");

            out.println("<html><head><title>" + this.getClass().getName() + "</title></head><body>");
            out.println("<h1>Person register</h1>");


            // Checks thats all fields are filled out.
            if (personName.equals(empty) == false || personAddress.equals(empty) == false || request.getParameter("personphone").equals(empty) == false) {
                personPhone = Long.valueOf(request.getParameter("personphone"));

                tx.begin();
                Person per1 = new Person(personName, personAddress, personPhone);
                em.persist(per1);

                out.println("A person with the following data has been registerd:");
                out.println("<br/>");
                out.println("Name: " + personName + ", Address: " + personAddress + ", Phone: " + personPhone);


                tx.commit();

            } else {
                out.println("All fields must be filled out!");
                out.println("<a href=registerperson.html>RegisterPerson</a><br/>");
            }
        } catch (Exception e) {
            out.println("<pre>");
            e.printStackTrace(out);
            out.println("</pre>");
        } finally {
            out.println("<hr/>");
            out.println("<p><a href=\"index.html\">Home</a></p>");

            out.println("</body>");
            out.println("</html>");
            em.close();
        }

        // 2 sec redirect.
        out.println("<meta http-equiv=Refresh content=2;url=adminPage.html> ");
        out.println("<br/>");
        out.println("Redirect in 2 sec.");
        out.close();
    }
}